import requests
from bs4 import BeautifulSoup
from novel.core import down_one_novel


# 任务
def job(config):
    # 保存目录
    save_path = config.get('Download', 'save_path')
    # 章节url前半部分
    real_server = config.get('Download', 'real_server')
    # 目录url
    real_target = config.get('Download', 'real_target')
    novel_name_tab = config.get('Download', 'novel_name_tab')

    # 获取小说名
    req = requests.get(url=real_target)
    req.encoding = req.apparent_encoding
    if ("GB2312" == req.encoding):  # gbk 为gb2312的扩充，对偏僻的汉字可以正常显示
        req.encoding = "gbk"
    html = req.text
    # xxx.txt
    title = BeautifulSoup(req.text, 'html.parser').find(novel_name_tab).string + '.txt'
    down_one_novel(real_server, real_target, save_path + title, config)
